package com.xmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xmall.bean.bo.AddOrDelete;
import com.xmall.bean.table.*;
import com.xmall.bean.vo.CollectDataVO;
import com.xmall.bean.vo.CollectVO;
import com.xmall.mapper.CollectMapper;
import com.xmall.mapper.GoodsMapper;
import com.xmall.service.CollectService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Connor
 * @ClassName: CollectServiceImpl
 * @PackageName: com.xmall.service.impl
 * @date 2020/12/11
 * @Version 1.0
 */
@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public CollectVO<CollectDataVO> listCollection(Integer page, Integer size, Byte type) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<CollectDataVO> collectDataVOS = new ArrayList<>();
        PageHelper.startPage(page, size);
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andUserIdEqualTo(user.getId());
        criteria.andTypeEqualTo(type);
        criteria.andDeletedEqualTo(false);
        List<Collect> collectList = collectMapper.selectByExample(collectExample);
        for (Collect collect : collectList) {
            Integer goodsId = collect.getValueId();
            Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
            collectDataVOS.add(new CollectDataVO(goods.getBrief(),
                    goods.getPicUrl(),
                    collect.getValueId(),
                    goods.getName(),
                    collect.getId(),
                    collect.getType(),
                    goods.getRetailPrice()));
        }
        return new CollectVO<>((int) new PageInfo<>(collectList).getTotal(), collectDataVOS);
    }

    /**
     * 高宁
     */
    @Override
    public Map<String, String> addOrDelete(AddOrDelete addOrDelete) {

        HashMap<String, String> map = new HashMap<>();
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Integer userId = user.getId();
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andUserIdEqualTo(userId).andValueIdEqualTo(addOrDelete.getValueId()).andDeletedEqualTo(false);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        if (collects.size() != 0) {
            Collect collect = new Collect();
            collect.setDeleted(true);
            collect.setUserId(userId);
            collect.setValueId(addOrDelete.getValueId());
            collect.setUpdateTime(new Date());
            collectMapper.updateByExampleSelective(collect,collectExample);
            map.put("type", "delete");
        } else {
            Collect collect = new Collect(null, userId, addOrDelete.getValueId(), addOrDelete.getType(), new Date(), new Date(), false);
            collectMapper.insert(collect);
            map.put("type", "add");
        }
        return map;
    }
}
